package com.yuji.aacar.controller.register;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.yuji.aacar.R;
import com.yuji.aacar.base.BaseActivity;
import com.yuji.aacar.bean.response.SendShortMessageResponse;
import com.yuji.aacar.bean.response.ValidatorShortMessageResponse;
import com.yuji.aacar.controller.about.AboutShowActivity;
import com.yuji.aacar.core.cores.event.CoreEventBusMessage;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.service.BaseServiceMediator;
import com.yuji.aacar.service.BaseServiceParams;
import com.yuji.aacar.tools.Tools;
import com.yuji.aacar.widgets.autolayout.AutoRelativeLayout;
import com.yuji.aacar.widgets.countdownbutton.WCountDownButton;
import com.yuji.aacar.widgets.edittext.CheckEditText;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.yuji.aacar.service.BaseServiceMediator.VALIDATOR_SHORT_MESSAGE;

public class RegisterFristActivity extends BaseActivity {


    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.back_image)
    AutoRelativeLayout backImage;
    @BindView(R.id.title_content)
    PingFangTextView titleContent;
    @BindView(R.id.back_login)
    PingFangTextView backLogin;
    @BindView(R.id.title_message_iv)
    ImageView titleMessageIv;
    @BindView(R.id.title_message_rl)
    AutoRelativeLayout titleMessageRl;
    @BindView(R.id.title_rl)
    AutoRelativeLayout titleRl;
    @BindView(R.id.register_phone_number_et)
    CheckEditText registerPhoneNumberEt;
    @BindView(R.id.clear_register_iv)
    ImageView clearRegisterIv;
    @BindView(R.id.register_verify_code_et)
    CheckEditText registerVerifyCodeEt;
    @BindView(R.id.countdowntv)
    WCountDownButton countdowntv;
    @BindView(R.id.register_next_btn)
    Button registerNextBtn;
    @BindView(R.id.register_service_page)
    PingFangTextView registerServicePage;


    private RegisterFristVM registerFristVM;
    private String phone;
    private String verifyCode;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_regist_first);
        ButterKnife.bind(this);
        initTitle(true, "注册", "登录");
        //初始化倒计时帮助类
        super.onCreate(savedInstanceState);

        registerPhoneNumberEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count > 0) {
                    clearRegisterIv.setVisibility(View.VISIBLE);
                } else {
                    clearRegisterIv.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        clearRegisterIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerPhoneNumberEt.setText("");
            }
        });

    }

    @Override
    public void alreadyBindBaseViewModel() {
        super.alreadyBindBaseViewModel();
        //        requestParams =
    }


    @Override
    public void handleMessage(CoreEventBusMessage message) {
        super.handleMessage(message);
    }

    @Override
    public void requestSuccess(CoreServiceResponse response) {
        super.requestSuccess(response);
        if (response.getRequestCode() == BaseServiceMediator.SEND_CHECK_CODE) {
            SendShortMessageResponse sendShortMessageResponse = (SendShortMessageResponse)response.getResponse();
            showToast(sendShortMessageResponse.getMessage());
//            UserCenter.saveCurrentUser((LoginResponse) response.getResponse());
//            AppRoute.getInstance().nextController(this, TabHomeActivity.class, AppRoute.WITHOUT_RESULTCODE);
        } else if(response.getRequestCode() == BaseServiceMediator.CHECK_CHECK_CODE){
            ValidatorShortMessageResponse sendShortMessageResponse = (ValidatorShortMessageResponse)response.getResponse();
            if (sendShortMessageResponse.isSuccess()){
                String nextStepCode = sendShortMessageResponse.getMessage();
                Intent intent = getIntent();
                intent.putExtra("phone", phone);
                intent.putExtra("code", nextStepCode);
                presentController(RegisterPasswordActivity.class, intent);
            }
        }
    }

    @Override
    public void requestFailed(CoreServiceResponse response) {
        super.requestFailed(response);
    }


    @OnClick({R.id.countdowntv, R.id.register_next_btn, R.id.register_service_page})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.countdowntv:
                if (isPhoneLegal()){
                    countdowntv.startCountDown();
                    requestParams = BaseServiceParams.sendShortMessage(phone, "0");
                    doTask(BaseServiceMediator.SEND_SHORT_MESSAGE);
                }
                break;
            case R.id.register_next_btn:
                if (isLegal()){
                    requestParams = BaseServiceParams.validatorshortmessage(phone, verifyCode, "0");
                    doTask(VALIDATOR_SHORT_MESSAGE);
                }
                break;
            case R.id.register_service_page:
                presentController(AboutShowActivity.class, getIntent().putExtra("from", 3));
                break;

        }
    }



    private boolean isPhoneLegal() {
        phone = registerPhoneNumberEt.getText().toString();
        if (!Tools.isPhoneNumberValid(phone)){
            showToast("请输入正确的手机号！");
            return false;
        }
        return true;
    }

    private boolean isLegal() {
        phone = registerPhoneNumberEt.getText().toString();
        verifyCode = registerVerifyCodeEt.getText().toString();
        if (!Tools.isPhoneNumberValid(phone)){
            showToast("请输入正确的手机号！");
            return false;
        }
        if (TextUtils.isEmpty(verifyCode)){
            showToast("请输入短信验证码！");
            return false;
        }
        return true;
    }

}
